package com.qf.dao;

import com.qf.pojo.Goods;
import com.qf.util.DBUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;
import java.util.concurrent.DelayQueue;

/**
 * @author LiXuekai on 2022/5/24
 */
public class GoodsDaoImpl implements GoodsDao {
    //创建dbutil工具类
    private QueryRunner queryRunner=new QueryRunner(DBUtil.getDateSource());
    @Override
    public void insertGoods(Goods goods) {
        String sql ="insert into tb_goods(name,pubdate,picture,price,star,intro,tid) value(?,?,?,?,?,?,?)";
        try {
            queryRunner.update(sql,goods.getName(),goods.getPubdate(),goods.getPicture(),goods.getPrice()
                                    ,goods.getStar(),goods.getIntro(),goods.getTid());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<Goods> findGoodsAll() {
        String sql = "select a.*, b.name typeName from tb_goods a " +
                "LEFT join tb_goods_type b ON a.tid = b.id";
        List<Goods> list = null;
        try {
            list = queryRunner.query(sql, new BeanListHandler<Goods>(Goods.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;

    }

    /**
     * 根据商品id 主键查询商品
     * @param id
     * @return
     */
    @Override
    public Goods findById(Integer id) {
        String sql="select * from tb_goods where id=?";
        try {
            Goods goods = queryRunner.query(sql, new BeanHandler<Goods>(Goods.class), id);
            return goods;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}